//
//  WDJVipController.m
//  wdj
//
//  Created by 蓝鳍互娱 on 2024/7/8.
//

#import "WDJVipController.h"

@interface WDJVipController ()
@property (nonatomic,strong) UILabel * vipPriceTitleLab;
@end

@implementation WDJVipController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = true;
    [self wdjCreatView];
    [self wdjUpdataVip];
}
- (void)wdjCreatView{
    UIImageView * vipBgImgView = [[UIImageView alloc] init];
    vipBgImgView.image = [WDjWholeData wholeImageNamed:@"wdj_VipBg"];
    vipBgImgView.userInteractionEnabled = true;
    [self.view addSubview:vipBgImgView];
    [vipBgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(0));
    }];
    UIImageView * vipHeadBgImgView = [[UIImageView alloc] init];
    vipHeadBgImgView.image = [WDjWholeData wholeImageNamed:@"wdj_VipHeadBg"];
    vipHeadBgImgView.userInteractionEnabled = true;
    [self.view addSubview:vipHeadBgImgView];
    [vipHeadBgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:21]));
        make.right.equalTo(@(-[WDjWholeData wholeScreewRatio:21]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:849]));
        make.top.equalTo(@([WDjWholeData wholeScreewRatio:44]+[WDjWholeData wholeTopSafeH]));
    }];
    UIButton * closeVipBtn = [[UIButton alloc] init];
    [closeVipBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_VipClose"] forState:UIControlStateNormal];
    [closeVipBtn addTarget:self action:@selector(closeVip) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:closeVipBtn];
    [closeVipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-[WDjWholeData wholeScreewRatio:24]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:64]));
        make.width.equalTo(@([WDjWholeData wholeScreewRatio:64]));
        make.top.equalTo(@([WDjWholeData wholeScreewRatio:12]+[WDjWholeData wholeTopSafeH]));
    }];
    UILabel * vipHeadTitleLab = [[UILabel alloc] init];
    vipHeadTitleLab.font = [UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:48] weight:UIFontWeightRegular];
    vipHeadTitleLab.textColor = [WDjWholeData wholeColorHexString:@"#FFFFFF"];
    vipHeadTitleLab.text = @"温度计";
    [self.view addSubview:vipHeadTitleLab];
    [vipHeadTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@([WDjWholeData wholeScreewRatio:613]+[WDjWholeData wholeTopSafeH]));
        make.centerX.equalTo(self.view);
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:60]));
    }];
    self.vipPriceTitleLab = [[UILabel alloc] init];
    self.vipPriceTitleLab.font = [UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:26] weight:UIFontWeightRegular];
    self.vipPriceTitleLab.textColor = [WDjWholeData wholeColorHexString:@"#FFFFFF"];
    self.vipPriceTitleLab.textAlignment = NSTextAlignmentCenter;
    self.vipPriceTitleLab.numberOfLines = 0;
    self.vipPriceTitleLab.text = @"免费试用3天，可自动续订，续费￥298/年，免费享受后续功能迭代，无需额外付费，随时取消";
    [self.view addSubview:self.vipPriceTitleLab];
    [self.vipPriceTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:60]));
        make.right.equalTo(@(-[WDjWholeData wholeScreewRatio:90]));
        make.top.equalTo(vipHeadTitleLab.mas_bottom).offset([WDjWholeData wholeScreewRatio:24]);
    }];
    UIImageView * vipHeadAlertImgView = [[UIImageView alloc] init];
    vipHeadAlertImgView.image = [WDjWholeData wholeImageNamed:@"wdj_VipAlert"];
    vipHeadAlertImgView.userInteractionEnabled = true;
    [self.view addSubview:vipHeadAlertImgView];
    [vipHeadAlertImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(@([WDjWholeData wholeScreewRatio:491]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:177]));
        make.top.equalTo(self.vipPriceTitleLab.mas_bottom).offset([WDjWholeData wholeScreewRatio:70]);
    }];
    UIButton * openVipBtn = [[UIButton alloc] init];
    [openVipBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_VipBuyBg"] forState:UIControlStateNormal];
    [openVipBtn addTarget:self action:@selector(openVip) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:openVipBtn];
    [openVipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:76]));
        make.right.equalTo(@(-[WDjWholeData wholeScreewRatio:76]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:86]));
        make.bottom.equalTo(@(-[WDjWholeData wholeScreewRatio:269]));
    }];
    UIButton * vipPriviceBtn = [[UIButton alloc] init];
    [vipPriviceBtn addTarget:self action:@selector(vipPrivice) forControlEvents:UIControlEventTouchUpInside];
    [vipPriviceBtn setTitle:@"隐私协议" forState:UIControlStateNormal];
    [vipPriviceBtn setTitleColor:[WDjWholeData wholeColorHexString:@"#01F2E7"] forState:UIControlStateNormal];
    vipPriviceBtn.titleLabel.font = [UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:24] weight:UIFontWeightRegular];
    [self.view addSubview:vipPriviceBtn];
    [vipPriviceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(@([WDjWholeData wholeScreewRatio:125]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:40]));
        make.top.equalTo(openVipBtn.mas_bottom).offset([WDjWholeData wholeScreewRatio:32]);
    }];
    UIButton * vipUserBtn = [[UIButton alloc] init];
    [vipUserBtn addTarget:self action:@selector(vipUser) forControlEvents:UIControlEventTouchUpInside];
    [vipUserBtn setTitle:@"用户协议" forState:UIControlStateNormal];
    [vipUserBtn setTitleColor:[WDjWholeData wholeColorHexString:@"#01F2E7"] forState:UIControlStateNormal];
    vipUserBtn.titleLabel.font = [UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:24] weight:UIFontWeightRegular];
    [self.view addSubview:vipUserBtn];
    [vipUserBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(vipPriviceBtn.mas_left);
        make.width.equalTo(@([WDjWholeData wholeScreewRatio:125]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:40]));
        make.top.equalTo(vipPriviceBtn);
    }];
    UIButton * vipUserPriceBtn = [[UIButton alloc] init];
    [vipUserPriceBtn addTarget:self action:@selector(vipUserPrice) forControlEvents:UIControlEventTouchUpInside];
    [vipUserPriceBtn setTitle:@"恢复购买" forState:UIControlStateNormal];
    [vipUserPriceBtn setTitleColor:[WDjWholeData wholeColorHexString:@"#01F2E7"] forState:UIControlStateNormal];
    vipUserPriceBtn.titleLabel.font = [UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:24] weight:UIFontWeightRegular];
    [self.view addSubview:vipUserPriceBtn];
    [vipUserPriceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(vipPriviceBtn.mas_right);
        make.width.equalTo(@([WDjWholeData wholeScreewRatio:125]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:40]));
        make.top.equalTo(vipPriviceBtn);
    }];
}
- (void)wdjUpdataVip{
    if([WDJPublicData defaultManager].wdjVipPrice){
        NSString * allPriceString = [NSString stringWithFormat:@"免费试用3天，可自动续订，续费￥%@/年，免费享受后续功能迭代，无需额外付费，随时取消",[WDJPublicData defaultManager].wdjVipPrice];
        NSString *priceString = [NSString stringWithFormat:@"续费￥%@/年",[WDJPublicData defaultManager].wdjVipPrice];
        NSRange priceRange = [allPriceString rangeOfString:priceString];
        
        NSMutableAttributedString *allPriceMutString = [[NSMutableAttributedString alloc] initWithString:allPriceString];
        [allPriceMutString addAttribute:NSForegroundColorAttributeName value:[WDjWholeData wholeColorHexString:@"#FFFFFF"] range:NSMakeRange(0,allPriceString.length)];
        [allPriceMutString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:26] weight:UIFontWeightRegular] range:NSMakeRange(0,allPriceString.length)];
        [allPriceMutString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:30] weight:UIFontWeightBold] range:priceRange];
        self.vipPriceTitleLab.attributedText = allPriceMutString;
    }
}
- (void)closeVip{
    [self dismissViewControllerAnimated:true completion:^{
            
    }];
}
- (void)openVip{
    WeakSelf
    [[WDJApplePayManger defaultRequest] beginApplePayProductId:[WDjWholeData wdjPublicForProductId]];
    [WDJApplePayManger defaultRequest].ApplePayScuessBlock = ^{
        NSDate *curDate = [NSDate date];
        NSCalendar *curCalendar = [NSCalendar currentCalendar];
        NSDateComponents *curDateComponent = [[NSDateComponents alloc] init];
        [curDateComponent setYear:1];
        NSDate *addDate = [curCalendar dateByAddingComponents:curDateComponent toDate:curDate options:0];
        NSTimeInterval curTime = [addDate timeIntervalSince1970];
        [[NSUserDefaults standardUserDefaults] setDouble:(double)curTime forKey:@"wdjVipBuyTime"];
        //maidian
        [WDJHttpHelper wdjMakePayDian];
        if(weakSelf.ApplePayScuessBlock){
            weakSelf.ApplePayScuessBlock();
        }
        [weakSelf dismissViewControllerAnimated:true completion:^{}];
    };
}
- (void)vipPrivice{
    WDJWebController * wdjWebCtrl = [[WDJWebController alloc] init];
    wdjWebCtrl.wdjWebUrl = [WDjWholeData wdjPublicForPriAgreement];
    wdjWebCtrl.isWhiteBack = false;
    [self.navigationController pushViewController:wdjWebCtrl animated:true];
}
- (void)vipUser{
    WDJWebController * wdjWebCtrl = [[WDJWebController alloc] init];
    wdjWebCtrl.wdjWebUrl = [WDjWholeData wdjPublicForUserAgreement];
    wdjWebCtrl.isWhiteBack = false;
    [self.navigationController pushViewController:wdjWebCtrl animated:true];
}
- (void)vipUserPrice{
    WeakSelf
    [[WDJApplePayManger defaultRequest] beginApplePayProductId:[WDjWholeData wdjPublicForProductId]];
    [WDJApplePayManger defaultRequest].ApplePayScuessBlock = ^{
        NSDate *curDate = [NSDate date];
        NSCalendar *curCalendar = [NSCalendar currentCalendar];
        NSDateComponents *curDateComponent = [[NSDateComponents alloc] init];
        [curDateComponent setYear:1];
        NSDate *addDate = [curCalendar dateByAddingComponents:curDateComponent toDate:curDate options:0];
        NSTimeInterval curTime = [addDate timeIntervalSince1970];
        [[NSUserDefaults standardUserDefaults] setDouble:(double)curTime forKey:@"wdjVipBuyTime"];
        if(weakSelf.ApplePayScuessBlock){
            weakSelf.ApplePayScuessBlock();
        }
        [weakSelf dismissViewControllerAnimated:true completion:^{}];
    };
}
@end
